package com.jboltai.capability.text;

import cn.hutool.core.util.StrUtil;
import com.jboltai.config.JBoltAIConfig;
import com.jboltai.event.text.ContentSourceType;
import com.jboltai.event.text.TextExtractEvent;
import com.jboltai.event.text.TextSplitEvent;
import com.jboltai.util.text.enums.FormatType;
import com.jboltai.util.text.enums.WebPageMode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;


/**
 * 文本内容工具类
 */
public class JBoltText {
    private static final Logger LOGGER = LoggerFactory.getLogger(JBoltAIConfig.LOGGER_NAME);

    /**
     * 从给定的file中提取文本
     * @param file
     * @return
     */
    public static TextExtractEvent extract(File file) {
        return new TextExtractEvent(file);
    }


    /**
     * 从给定的url中提取文本
     * @param url
     * @return
     */
    public static TextExtractEvent extract(String url) {
        return new TextExtractEvent(url);
    }
    /**
     * 从网址提取内容
     * @param webPageUrl 网页地址
     * @param formatType 提取后的网页代码要如何格式化
     * @return
     */
    public static TextExtractEvent extract(String webPageUrl , FormatType formatType  ) {
        return new TextExtractEvent(webPageUrl).setWebPageMode(WebPageMode.SYNC)
                .setHtmlFormatType(formatType).setContentSourceType(ContentSourceType.WEB_PAGE);
    }
    /**
     * 从网址提取内容
     * @param webPageUrl 网页地址
     * @param cssSelector 要提取内容的css选择器
     * @param formatType  提取后的网页代码要如何格式化
     * @return
     */
    public static TextExtractEvent extract(String webPageUrl, String cssSelector, FormatType formatType  ) {
        return new TextExtractEvent(webPageUrl).setWebPageMode(WebPageMode.SYNC)
                .setContentSelector(cssSelector).setHtmlFormatType(formatType).setContentSourceType(ContentSourceType.WEB_PAGE);
    }
    /**
     * 从网址提取内容
     * @param webPageUrl 网页地址
     * @param cssSelector 要提取内容的css选择器
     * @param webPageMode 网页模式，是同步渲染还是异步渲染的
     * @param formatType  提取后的网页代码要如何格式化
     * @return
     */
    public static TextExtractEvent extract(String webPageUrl, String cssSelector, WebPageMode webPageMode, FormatType formatType  ) {
        return new TextExtractEvent(webPageUrl).setWebPageMode(webPageMode)
                .setContentSelector(cssSelector).setHtmlFormatType(formatType).setContentSourceType(ContentSourceType.WEB_PAGE);
    }





    /**
     * 将文件文本内容进行分块处理
     * @param file
     * @return
     */
    public static TextSplitEvent splitFile(File file) {
        return new TextSplitEvent().setFile(file);
    }

    /**
     * 将文件文本内容进行分块处理
     * @param file
     * @param chunkSize 分块大小
     * @param chunkOverlap 分块重叠大小
     * @return
     */
    public static TextSplitEvent splitFile(File file, int chunkSize, int chunkOverlap) {
        if (2*chunkOverlap + 1 > chunkSize) throw new IllegalArgumentException("chunkOverlap不能超过chunkSize的一半" );
        return new TextSplitEvent().setFile(file).setChunkSize(chunkSize).setChunkOverlap(chunkOverlap);
    }

    /**
     * 将文件文本内容进行分块处理
     * @param file
     * @param chunkSize 分块大小
     * @param chunkOverlap 分块重叠大小
     * @param separators 分隔符
     * @return
     */
    public static TextSplitEvent splitFile(File file, int chunkSize, int chunkOverlap, String... separators) {
        if (2 * chunkOverlap + 1 > chunkSize) throw new IllegalArgumentException("chunkOverlap不能超过chunkSize的一半" );
        TextSplitEvent event = new TextSplitEvent();
        return event.setFile(file).setChunkSize(chunkSize).setChunkOverlap(chunkOverlap).setSeparators(separators);

    }

    /**
     * 将文件文本内容进行分块处理
     * @param file
     * @param separators 分隔符
     * @return
     */
    public static TextSplitEvent splitFile(File file, String... separators) {
        return new TextSplitEvent().setFile(file).setSeparators(separators);
    }
    /**
     * 将url文件文本内容进行分块处理
     * @param url
     * @return
     */
    public static TextSplitEvent splitUrl(String url) {
        return new TextSplitEvent().setUrl(url);
    }

    /**
     * 从网址提取内容并分割
     * @param webPageUrl 网页地址
     * @param cssSelector 要提取内容的css选择器
     * @param formatType  提取后的网页代码要如何格式化
     * @return
     */
    public static TextSplitEvent splitUrl(String webPageUrl, String cssSelector,  FormatType formatType  ) {
        return new TextSplitEvent().setUrl(webPageUrl).setWebPageMode(WebPageMode.SYNC)
                .setContentSelector(cssSelector).setHtmlFormatType(formatType).setContentSourceType(ContentSourceType.WEB_PAGE);
    }
    /**
     * 从网址提取内容并分割
     * @param webPageUrl 网页地址
     * @param cssSelector 要提取内容的css选择器
     * @param webPageMode 网页模式，是同步渲染还是异步渲染的
     * @param formatType  提取后的网页代码要如何格式化
     * @return
     */
    public static TextSplitEvent splitUrl(String webPageUrl, String cssSelector, WebPageMode webPageMode, FormatType formatType  ) {
        return new TextSplitEvent().setUrl(webPageUrl).setWebPageMode(webPageMode)
                .setContentSelector(cssSelector).setHtmlFormatType(formatType).setContentSourceType(ContentSourceType.WEB_PAGE);
    }

    /**
     * 将url文件文本内容进行分块处理
     * @param url
     * @param chunkSize 分块大小
     * @param chunkOverlap 分块重叠大小
     * @return
     */
    public static TextSplitEvent splitUrl(String url, int chunkSize, int chunkOverlap) {
        if (StrUtil.isBlank(url)) {
            throw new IllegalArgumentException("url不能为空");
        }
        if (2*chunkOverlap + 1 > chunkSize) throw new IllegalArgumentException("chunkOverlap不能超过chunkSize的一半" );
        return new TextSplitEvent().setUrl(url).setChunkSize(chunkSize).setChunkOverlap(chunkOverlap);
    }

    /**
     * 从网址提取内容并分割
     * @param webPageUrl 网页地址
     * @param cssSelector 要提取内容的css选择器
     * @param formatType  提取后的网页代码要如何格式化
     * @param chunkSize 分块大小
     * @param chunkOverlap 分块重叠大小
     * @return
     */
    public static TextSplitEvent splitUrl(String webPageUrl, String cssSelector, FormatType formatType , int chunkSize, int chunkOverlap ) {
        if (StrUtil.isBlank(webPageUrl)) {
            throw new IllegalArgumentException("url不能为空");
        }
        if (2*chunkOverlap + 1 > chunkSize) throw new IllegalArgumentException("chunkOverlap不能超过chunkSize的一半" );
        return new TextSplitEvent().setUrl(webPageUrl).setWebPageMode(WebPageMode.SYNC)
                .setContentSelector(cssSelector).setHtmlFormatType(formatType).setContentSourceType(ContentSourceType.WEB_PAGE).setChunkSize(chunkSize).setChunkOverlap(chunkOverlap);
    }
    /**
     * 从网址提取内容并分割
     * @param webPageUrl 网页地址
     * @param cssSelector 要提取内容的css选择器
     * @param webPageMode 网页模式，是同步渲染还是异步渲染的
     * @param formatType  提取后的网页代码要如何格式化
     * @param chunkSize 分块大小
     * @param chunkOverlap 分块重叠大小
     * @return
     */
    public static TextSplitEvent splitUrl(String webPageUrl, String cssSelector, WebPageMode webPageMode, FormatType formatType , int chunkSize, int chunkOverlap ) {
        if (StrUtil.isBlank(webPageUrl)) {
            throw new IllegalArgumentException("url不能为空");
        }
        if (2*chunkOverlap + 1 > chunkSize) throw new IllegalArgumentException("chunkOverlap不能超过chunkSize的一半" );
        return new TextSplitEvent().setUrl(webPageUrl).setWebPageMode(webPageMode)
                .setContentSelector(cssSelector).setHtmlFormatType(formatType).setContentSourceType(ContentSourceType.WEB_PAGE).setChunkSize(chunkSize).setChunkOverlap(chunkOverlap);
    }

    /**
     * 将url文件文本内容进行分块处理
     * @param url
     * @param chunkSize 分块大小
     * @param chunkOverlap 分块重叠大小
     * @param separators 分隔符
     * @return
     */
    public static TextSplitEvent splitUrl(String url, int chunkSize, int chunkOverlap, String... separators) {
        if (2 * chunkOverlap + 1 > chunkSize) throw new IllegalArgumentException("chunkOverlap不能超过chunkSize的一半" );
        TextSplitEvent event = new TextSplitEvent();
        return event.setUrl(url).setChunkSize(chunkSize).setChunkOverlap(chunkOverlap).setSeparators(separators);

    }

    /**
     * 将url文件文本内容进行分块处理
     * @param url
     * @param separators 分隔符
     * @return
     */
    public static TextSplitEvent splitUrl(String url, String... separators) {
        return new TextSplitEvent().setUrl(url).setSeparators(separators);
    }
    /**
     * 将文本内容进行分块处理
     * @param text
     * @return
     */
    public static TextSplitEvent splitText(String text) {
        return new TextSplitEvent().setOriText(text);
    }

    /**
     * 将文本内容进行分块处理
     * @param text
     * @param chunkSize 分块大小
     * @param chunkOverlap 分块重叠大小
     * @return
     */
    public static TextSplitEvent splitText(String text, int chunkSize, int chunkOverlap) {
        if (2*chunkOverlap + 1 > chunkSize) throw new IllegalArgumentException("chunkOverlap不能超过chunkSize的一半" );
        return new TextSplitEvent().setOriText(text).setChunkSize(chunkSize).setChunkOverlap(chunkOverlap);
    }

    /**
     * 将文本内容进行分块处理
     * @param text
     * @param chunkSize 分块大小
     * @param chunkOverlap 分块重叠大小
     * @param separators 分隔符
     * @return
     */
    public static TextSplitEvent splitText(String text, int chunkSize, int chunkOverlap, String... separators) {
        if (2 * chunkOverlap + 1 > chunkSize) throw new IllegalArgumentException("chunkOverlap不能超过chunkSize的一半" );
        return new TextSplitEvent().setOriText(text).setChunkSize(chunkSize).setChunkOverlap(chunkOverlap).setSeparators(separators);

    }

    /**
     * 将文本内容进行分块处理
     * @param text
     * @param separators 分隔符
     * @return
     */
    public static TextSplitEvent splitText(String text, String... separators) {
        return new TextSplitEvent().setOriText(text).setSeparators(separators);
    }


}
